package com.example.aqarapp.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.view.Display;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import aqarapp.R;

import java.util.ArrayList;

public class OutletDialog extends Dialog {
    private ArrayList<String> dataList;
    private Context mContext;
    private Point mWindowSize = new Point();

    NumberPickerView mHour_pv;
    NumberPickerView mMinute_pv;
    ConstraintLayout v1;
    ConstraintLayout v2;
    private Button start_btn;
    private TextView mCountTimeText;

    public OutletDialog(Context context) {
        super(context, R.style.OutletDialog);
        mContext = context;
    }

    public OutletDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected OutletDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.outlet_dialog);
        WindowManager windowManager = ((Activity) mContext).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        display.getSize(mWindowSize);
        lp.width = mWindowSize.x * 4 / 5;// 设置dialog宽度为屏幕的4/5
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(true);//点击外部Dialog消失
        v1 = (ConstraintLayout) findViewById(R.id.settime);


        initBulbScenesView();
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        return super.onTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    public void setHour(int hour) {
        mHour_pv.setValue(hour);
        if (hour != 0) {
            start_btn.setEnabled(true);
        }
    }

    public void setMinute(int minute) {
        mMinute_pv.setValue(minute);
        if (minute == 0) {
            mCountTimeText.setText("01时00分");
        }
        else {
            mCountTimeText.setText("00时"+minute+"分");
        }
        if (minute != 0) {
            start_btn.setEnabled(true);
        }
    }

    public int getSetTimeValue() {
        int hour = mHour_pv.getValue();
        int minute = mMinute_pv.getValue();
        return hour*3600+minute*60;
    }

    public boolean getStartBtnEnabled() {
        return start_btn.isEnabled();
    }

    private void initBulbScenesView() {
        setContentView(R.layout.outlet_dialog);

        mHour_pv = (NumberPickerView) findViewById(R.id.minute_pv);
        mMinute_pv = (NumberPickerView) findViewById(R.id.second_pv);
        mCountTimeText = (TextView) findViewById(R.id.show_time);
        mHour_pv.setMaxValue(23);
        mHour_pv.setMinValue(0);
        mMinute_pv.setMaxValue(59);
        mMinute_pv.setMinValue(0);
        mHour_pv.setOnValueChangedListener(new NumberPickerView.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPickerView picker, int oldVal, int newVal) {
                String hour;
                String minute;
                int value = mMinute_pv.getValue();
                if (value < 10) {
                    minute = "0"+value;
                }
                else {
                    minute = String.valueOf(value);
                }
                if (newVal < 10) {
                    hour = "0"+newVal;
                }
                else {
                    hour = String.valueOf(newVal);
                }
                mCountTimeText.setText(hour+"时"+minute+"分");
                if (newVal != 0 || value != 0) {
                    start_btn.setEnabled(true);
                }
                else {
                    start_btn.setEnabled(false);
                }
            }
        });
        mMinute_pv.setOnValueChangedListener(new NumberPickerView.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPickerView picker, int oldVal, int newVal) {
                String hour;
                String minute;
                int value = mHour_pv.getValue();
                if (value < 10) {
                    hour = "0"+value;
                }
                else {
                    hour = String.valueOf(value);
                }
                if (newVal < 10) {
                    minute = "0"+newVal;
                }
                else {
                    minute = String.valueOf(newVal);
                }
                mCountTimeText.setText(hour+"时"+minute+"分");
                if (newVal != 0 || value != 0) {
                    start_btn.setEnabled(true);
                }
                else {
                    start_btn.setEnabled(false);
                }
            }
        });

        start_btn = (Button) findViewById(R.id.start_btn);
        start_btn.setEnabled(false);
//        mHour_pv.setFormatter(new NumberPicker.Formatter() {
//            @Override
//            public String format(int value) {
//
//                String data;
//
//                if (value < 10) {
//                    data = "0" + value;//让小于10的数前面加个0再输出
//                } else {
//                    data = String.valueOf(value); //大于10的数就不变
//                }
//
//                return data;
//            }
//        });
//        mMinute_pv.setFormatter(new NumberPicker.Formatter() {
//            @Override
//            public String format(int value) {
//
//                String data;
//
//                if (value < 10) {
//                    data = "0" + value;//让小于10的数前面加个0再输出
//                } else {
//                    data = String.valueOf(value); //大于10的数就不变
//                }
//
//                return data;
//            }
//        });
//        mHour_pv.setWrapSelectorWheel(true);
//        mMinute_pv.setWrapSelectorWheel(true);
////        mHour_pv.setOnScrollListener(this);
//        mHour_pv.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
//        mMinute_pv.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
//        mHour_pv.setNumberPickerMidTextSize(30);
//        mMinute_pv.setNumberPickerMidTextSize(30);
//        mHour_pv.setNumberPickerDividerColor(0);
//        mMinute_pv.setNumberPickerDividerColor(0);
//        List<String> data = new ArrayList<String>();
//        List<String> seconds = new ArrayList<String>();
//        for (int i = 0; i < 24; i++)
//        {
//            data.add(i < 10 ? "0" + i : "" + i);
//        }
//        for (int i = 0; i < 60; i++)
//        {
//            seconds.add(i < 10 ? "0" + i : "" + i);
//        }
//        mHour_pv.setData(data);
//        mHour_pv.setOnSelectListener(new PickerView.onSelectListener()
//        {
//
//            @Override
//            public void onSelect(String text)
//            {
////                Toast.makeText(MainActivity.this, "选择了 " + text + " 分",
////                        Toast.LENGTH_SHORT).show();
//            }
//        });
//        mMinute_pv.setData(seconds);
//        mMinute_pv.setOnSelectListener(new PickerView.onSelectListener()
//        {
//
//            @Override
//            public void onSelect(String text)
//            {
////                Toast.makeText(MainActivity.this, "选择了 " + text + " 秒",
////                        Toast.LENGTH_SHORT).show();
//            }
//        });

    }
}
